//
//  RouteRequest.m
//  Cycling
//
//  Created by cjg.mini on 2023/4/8.
//

#import "RouteRequest.h"
#import "BaseBodyModel.h"

@implementation RouteRequest

// 获取路线列表
+ (void)getRouteListWithPage:(NSInteger)page pageSize:(NSInteger)pageSize key:(NSString *)key type:(NSString *)type lat:(NSString *)lat lng:(NSString *)lng result:(void(^)(RouteListModel *model))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_RouteList];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:@(page) forKey:@"page"];
    [parm setObject:@(pageSize) forKey:@"pageSize"];
    [parm setObject:key forKey:@"key"];
    [parm setObject:type forKey:@"type"];
    [parm setObject:lat forKey:@"lat"];
    [parm setObject:lng forKey:@"lng"];
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    RouteListModel *list = [RouteListModel modelWithJSON:model.data];
                    resultBlock(list);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
            
        }
    }];

}

// 路线详情
+ (void)getRouteDetailWithId:(NSString *)routeId lat:(NSString *)lat lng:(NSString *)lng result:(void(^)(RouteDetailModel *model))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_RouteDetail];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:routeId forKey:@"id"];
    [parm setObject:lat forKey:@"lat"];
    [parm setObject:lng forKey:@"lng"];
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    RouteDetailModel *detal = [RouteDetailModel mj_objectWithKeyValues:model.data[@"detail"]];
                    resultBlock(detal);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
            
        }
    }];
}
// 添加路线
+ (void)addRouteWithRouteId:(NSString *)routeId name:(NSString *)name result:(void(^)(NSDictionary *ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_RouteAdd];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:routeId forKey:@"id"];
    [parm setObject:name forKey:@"name"];

    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    NSDictionary *dic = model.data;
                    resultBlock(dic);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
            
        }
    }];
}
// 取消路线
+ (void)cancleRouteWithRouteId:(NSString *)routeId result:(void(^)(NSDictionary *ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_RouteCancle];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:routeId forKey:@"id"];
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    NSDictionary *dic = model.data;
                    resultBlock(dic);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
        }
    }];
}
// 评论路线
+ (void)commentsRouteWithRouteId:(NSString *)routeId text:(NSString *)text recid:(NSString *)recid result:(void(^)(BOOL success))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_RouteComments];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:routeId forKey:@"id"];
    [parm setObject:text forKey:@"text"];
    if (recid.length != 0) {
        [parm setObject:recid forKey:@"recid"];
    }
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(NO);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    NSDictionary *dic = model.data;
                    resultBlock(YES);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(NO);
                }
            }
            
        }
    }];
}
// 点赞路线
+ (void)praiseRouteWithRouteId:(NSString *)routeId result:(void(^)(NSDictionary *ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_RoutePraise];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:routeId forKey:@"id"];
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    NSDictionary *dic = model.data;
                    if(!dic) {
                        dic = @{@"success" : @1};
                    }
                    resultBlock(dic);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
            
        }
    }];
}
// 取消点赞
+ (void)cancelPraiseRouteWithRouteId:(NSString *)routeId result:(void(^)(NSDictionary *ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_RouteCancelPraise];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:routeId forKey:@"id"];
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    NSDictionary *dic = model.data;
                    if(!dic) {
                        dic = @{@"success" : @1};
                    }
                    resultBlock(dic);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
            
        }
    }];
}
// 收藏路线
+ (void)collectionRouteWithRouteId:(NSString *)routeId result:(void(^)(NSDictionary *ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_RouteCollection];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:routeId forKey:@"id"];
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    NSDictionary *dic = model.data;
                    if(!dic) {
                        dic = @{@"success" : @1};
                    }
                    resultBlock(dic);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
            
        }
    }];
}
// 取消收藏路线
+ (void)cancleCollectionRouteWithRouteId:(NSString *)routeId result:(void(^)(NSDictionary *ret))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_RouteCancelCollection];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:routeId forKey:@"id"];
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    NSDictionary *dic = model.data;
                    if(!dic) {
                        dic = @{@"success" : @1};
                    }
                    resultBlock(dic);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
        }
    }];
}

// 骑行线路详情
+ (void)getCyclingRouteDetailWithId:(NSInteger)cid result:(void(^)(CyclingRouteDetailModel *model))resultBlock
{
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_RouteCyclingRouteDetail];
    NSMutableDictionary *parm = [NSMutableDictionary dictionary];
    [parm setObject:@(cid) forKey:@"id"];
    
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(error) {
            NSError *underlineError = [error.userInfo valueForKey:NSUnderlyingErrorKey];
            if(underlineError) {
                NSData *errorData = [underlineError.userInfo valueForKey:@"com.alamofire.serialization.response.error.data"];
                NSString *errorString = [[NSString alloc] initWithData:errorData encoding:NSUTF8StringEncoding];
                if(errorString) {
                    DebugLog(@"UnderlineError: %@", errorString);
                }
            }
        }
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    NSDictionary *dic = [model.data valueForKey:@"detail"];
                    CyclingRouteDetailModel *detailModel = [CyclingRouteDetailModel modelWithJSON:dic];
                    resultBlock(detailModel);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                }
            }
        }
    }];
    
}

//添加私人路线
+ (void)postAddPersonalRouteWithParams:(NSDictionary *)params result:(void(^)(BOOL success, NSDictionary *data, NSString *reason))complete
{
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_RouteAdd];
    
    [self basePOST:url cache:NO autoHideLoading:NO parms:params result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(complete){
            if (resultObject == nil) {
                complete(NO, nil, @"数据未返回");
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    complete(YES, model.data, model.msg);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    complete(NO, nil, model.msg);
                }
            }
        }
    }];
    
}
//编辑私人路线
+ (void)postEidtPersonalRouteWithParams:(NSDictionary *)params result:(void(^)(BOOL success, NSDictionary *data, NSString *reason))complete {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Cyc_API_RouteEidt];
    
    [self basePOST:url cache:NO autoHideLoading:NO parms:params result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(complete){
            if (resultObject == nil) {
                complete(NO, nil, @"数据未返回");
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    complete(YES, model.data, model.msg);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    complete(NO, nil, model.msg);
                }
            }
        }
    }];
}

+ (void)getCommentListWith:(NSInteger)page pid:(NSString *)pid isSuper:(BOOL)isSuper result:(void(^)(BOOL success, NSDictionary *data, NSString *reason))complete {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,isSuper ? Cyc_API_RouteCommentList : Cyc_API_RouteEvaluationlist];
    
    [self basePOST:url cache:NO autoHideLoading:NO parms:@{@"page":@(page),@"pid":pid,@"pageSize":@"10"} result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(complete){
            if (resultObject == nil) {
                complete(NO, nil, @"数据未返回");
            } else {
                BaseBodyModel *model = [BaseBodyModel modelWithJSON:resultObject];
                if (model.code == 1) {
                    complete(YES, model.data, model.msg);
                } else {
                    [HUDHelper showHudTipStr:model.msg];
                    complete(NO, nil, model.msg);
                }
            }
        }
    }];
}
@end
